﻿@inject IDataConfigService DataConfigService

<MudDialog>
    <DialogContent>
        <MudContainer Style="max-height: 480px; overflow-y: scroll">
            <MudForm @ref="_editForm" Model="EditData" Validation="@(_validator.ValidateValue)" ValidationDelay="0">
                <MudGrid>
                    <MudItem xs="6">
                        <MudTextField @bind-Value="EditData.FieldCode" For="@(() => EditData.FieldCode)" Label="字段编码"></MudTextField>
                    </MudItem>
                    <MudItem xs="6">
                        <MudTextField @bind-Value="EditData.FieldName" For="@(() => EditData.FieldName)" Label="字段名称"></MudTextField>
                    </MudItem>
                    <MudItem xs="6">
                        <MudSelect T="string" Value="EditData.DataType" ValueChanged="DataTypeChangedHandler"
                                   For="@(() => EditData.DataType)" Label="数据类型" Dense="true" AnchorOrigin="Origin.BottomCenter">
                            <MudSelectItem Value="@("NONE")"></MudSelectItem>
                            <MudSelectItem Value="@("COLUMN_VARCHAR")">字符串</MudSelectItem>
                            <MudSelectItem Value="@("COLUMN_NUMBER")">数值</MudSelectItem>
                        </MudSelect>
                    </MudItem>
                    <MudItem xs="6">
                        <MudSelectList TItem="ConfigColMetaInfoResponseDto" TValue="string" Items="_colValues"
                                       TextField="@(item => item.ColumnName)"
                                       ValueField="@(item => item.ColumnCode)"
                                       @bind-Value="EditData.ValueColumnMap" For="@(() => EditData.ValueColumnMap)" Label="存储字段" Dense="true" AnchorOrigin="Origin.BottomCenter">
                        </MudSelectList>
                    </MudItem>
                    <MudItem xs="6">
                        <MudSelect @bind-Value="EditData.IsRequired" For="@(() => EditData.IsRequired)" Label="必填" Dense="true" AnchorOrigin="Origin.BottomCenter">
                            <MudSelectItem Value="true">是</MudSelectItem>
                            <MudSelectItem Value="false">否</MudSelectItem>
                        </MudSelect>
                    </MudItem>
                    <MudItem xs="6">
                        <MudSelect @bind-Value="EditData.IsKey" For="@(() => EditData.IsKey)" Label="主键" Dense="true" AnchorOrigin="Origin.BottomCenter">
                            <MudSelectItem Value="true">是</MudSelectItem>
                            <MudSelectItem Value="false">否</MudSelectItem>
                        </MudSelect>
                    </MudItem>
                    <MudItem xs="6">
                        <MudSelect @bind-Value="EditData.IsVisible" For="@(() => EditData.IsVisible)" Label="可见" Dense="true" AnchorOrigin="Origin.BottomCenter">
                            <MudSelectItem Value="true">是</MudSelectItem>
                            <MudSelectItem Value="false">否</MudSelectItem>
                        </MudSelect>
                    </MudItem>
                    <MudItem xs="6">
                        <MudSelect @bind-Value="EditData.IsSearch" For="@(() => EditData.IsSearch)" Label="查询条件" Dense="true" AnchorOrigin="Origin.BottomCenter">
                            <MudSelectItem Value="true">是</MudSelectItem>
                            <MudSelectItem Value="false">否</MudSelectItem>
                        </MudSelect>
                    </MudItem>
                    <MudItem xs="6">
                        <MudSelect @bind-Value="EditData.SearchRequired" For="@(() => EditData.SearchRequired)" Label="查询必填" Dense="true" AnchorOrigin="Origin.BottomCenter">
                            <MudSelectItem Value="true">是</MudSelectItem>
                            <MudSelectItem Value="false">否</MudSelectItem>
                        </MudSelect>
                    </MudItem>
                    <MudItem xs="6">
                        <MudSelect @bind-Value="EditData.OrderBy" For="@(() => EditData.OrderBy)" Label="查询结果排序" Dense="true" AnchorOrigin="Origin.BottomCenter">
                            <MudSelectItem Value="@("None")"></MudSelectItem>
                            <MudSelectItem Value="@("Asc")">升序</MudSelectItem>
                            <MudSelectItem Value="@("Desc")">降序</MudSelectItem>
                        </MudSelect>
                    </MudItem>
                    <MudItem xs="6">
                        <MudTextField @bind-Value="EditData.DefaultValue" For="@(() => EditData.DefaultValue)" Label="默认值"></MudTextField>
                    </MudItem>
                    <MudItem xs="6">
                        <MudNumericField @bind-Value="EditData.Sort" For="@(() => EditData.Sort)" Label="排序"></MudNumericField>
                    </MudItem>
                    <MudItem xs="6">
                        <MudTextField @bind-Value="EditData.Description" For="@(() => EditData.Description)" Label="描述"></MudTextField>
                    </MudItem>
                    <MudItem xs="6">
                        <MudSelect T="string" Value="EditData.ControlType"
                                   ValueChanged="ControlTypeChangedHandle"
                                   For="@(() => EditData.ControlType)" Label="控件类型"
                                   Dense="true" AnchorOrigin="Origin.BottomCenter">
                            <MudSelectItem Value="@("None")"></MudSelectItem>
                            <MudSelectItem Value="@("ControlTextBox")">文本框控件</MudSelectItem>
                            <MudSelectItem Value="@("ControlSelect")">下拉框控件</MudSelectItem>
                            <MudSelectItem Value="@("ControlCascadingSelect")">级联下拉框控件</MudSelectItem>
                            <MudSelectItem Value="@("ControlNumeric")">数值框控件</MudSelectItem>
                            <MudSelectItem Value="@("ControlDate")">日期控件</MudSelectItem>
                        </MudSelect>
                    </MudItem>
                    <CascadingValue Value="EditData" IsFixed="true">
                        <DynamicComponent Type="_selectedComponent">
                        </DynamicComponent>
                    </CascadingValue>
                </MudGrid>
            </MudForm>
        </MudContainer>
    </DialogContent>
    <DialogActions>
        <MudButton Variant="Variant.Filled" OnClick="Cancel">取消</MudButton>
        <MudButton Color="Color.Primary" Variant="Variant.Filled" OnClick="Submit">保存</MudButton>
    </DialogActions>
</MudDialog>


@code {
    private MudForm? _editForm = new();
    private ConfigFieldEditValidator _validator = new();

    private IList<ConfigColMetaInfoResponseDto> _colValues = new List<ConfigColMetaInfoResponseDto>();

    private Type _selectedComponent = typeof(FieldEmptyPartial);

    [Parameter]
    public Guid? Key { get; set; }

    [Parameter]
    public string? ConfigCode { get; set; }

    [Parameter]
    [NotNull]
    public ConfigFieldEditRequestDto? EditData { get; set; }

    [Parameter]
    public Func<Task>? SubmitSuccessCallback { get; set; }

    [CascadingParameter]
    MudDialogInstance? MudDialog { get; set; }

    async Task Submit()
    {
        if (_editForm != null && (!string.IsNullOrEmpty(ConfigCode)))
        {
            await _editForm.Validate();

            if (_editForm.IsValid)
            {
                if (Key != null)
                {
                    await DataConfigService.UpdateDataConfigField(Key.Value, EditData);
                }
                else
                {
                    await DataConfigService.CreateDataConfigField(ConfigCode, EditData);
                }

                MudDialog?.Close(DialogResult.Ok(true));
            }
        }
    }

    void Cancel() => MudDialog?.Cancel();

    async Task DataTypeChangedHandler(string value)
    {
        EditData.DataType = value;
        EditData.ValueColumnMap = string.Empty;

        await BindValueColumnMapSelect(value);
    }

    async Task BindValueColumnMapSelect(string value)
    {
        _colValues = await DataConfigService.QueryAvailableColumnByDataType(value);
    }

    void ControlTypeChangedHandle(string value)
    {
        if (!string.IsNullOrEmpty(value))
        {
            EditData.ControlType = value;
            GetCascadingType(value);
        }
    }

    void GetCascadingType(string? controlType)
    {
        switch (controlType)
        {
            case "ControlTextBox":
                _selectedComponent = typeof(FieldTextBoxPartial);
                break;
            case "ControlSelect":
                _selectedComponent = typeof(FieldSelectPartial);
                break;
            case "ControlNumeric":
                _selectedComponent = typeof(FieldNumericPartial);
                break;
            case "ControlDate":
                _selectedComponent = typeof(FieldDatePartial);
                break;
            default:
                _selectedComponent = typeof(FieldEmptyPartial);
                break;
        }
    }

    protected override async Task OnParametersSetAsync()
    {
        EditData = new ConfigFieldEditRequestDto();
        if (Key != null)
        {
            var result = await DataConfigService.GetDataConfigField(Key.Value);
            if (result.Succeeded && result.Data != null)
            {
                ControlTypeChangedHandle(result.Data.ControlType);
                await BindValueColumnMapSelect(result.Data.DataType);

                EditData.FieldCode = result.Data.FieldCode;
                EditData.FieldName = result.Data.FieldName;
                EditData.DataType = result.Data.DataType;
                EditData.ValueColumnMap = result.Data.ValueColumnMap;
                EditData.IsRequired = result.Data.IsRequired;
                EditData.IsKey = result.Data.IsKey;
                EditData.IsVisible = result.Data.IsVisible;
                EditData.IsSearch = result.Data.IsSearch;
                EditData.Sort = result.Data.Sort;
                EditData.Description = result.Data.Description;
                EditData.ControlType = result.Data.ControlType;
                EditData.MaxLengthTextBox = result.Data.MaxLengthTextBox;
                EditData.RegexExpTextBox = result.Data.RegexExpTextBox;
                EditData.RegexExpDescTextBox = result.Data.RegexExpDescTextBox;
                EditData.DataSourceTypeSelect = result.Data.DataSourceTypeSelect;
                EditData.DataSourceStaticValueSelect = result.Data.DataSourceStaticValueSelect;
            }
        }
    }

}